<template>
	<view class="main-cont">
		<view class="content">
			<view class="box box-tb box-align-center box-pack-center">
				<view class="shz" v-show="regeditstatus==0">
					<image src="../../static/shc.png" mode="" class="statusimg"></image>
					<view class="bo">
						审核中
					</view>
					<view class="bo">
						请耐心等待...
					</view>
				</view>
				<view class="shz" v-show="regeditstatus==1">
					<view class="bo">
						已审核通过！登录密码为手机号！
					</view>
				</view>
				<view class="shenhe" v-show="regeditstatus==2">
					<image src="../../static/bo2.png" mode="" class="statusimg"></image>
					<view class="bo">您的申请已被驳回</view>
					<view class="shr">审核人：{{regedit.approvor}}</view>
					<view class="fankun"></view>
					<view class="text">
						驳回原因：{{regedit.reason}}
					</view>
				</view>
				<view class="register" @click="toindex">返回</view>
				<u-toast ref="uToast" />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: '',
				reason: '',
				current: '',
				icon: '',
				regedit: uni.getStorageSync("regedit"),
				regeditstatus: uni.getStorageSync("regeditstatus"),
				timer: '',
				unionid: uni.getStorageSync("unionid"),
				mobile: uni.getStorageSync("mobile")
			}
		},
		onLoad(opt) {
			//console.log(">>")
			this.timer = setInterval(() => {
				this.getresult()
			}, 3000);
		},
		methods: {
			getresult() {
				//console.log(">>")
				this.$api.post({
					url: '/regedit/getResultByMobile',method:'post'
				}, {
					mobile: this.mobile
				}).then(res => {
					//console.log(res.result)
					this.regedit = res.result;
					this.regeditstatus = res.result.status;
					if (res.result.status == 1) {
						clearInterval(this.timer);
						this.$refs.uToast.show({
							title: '已通过审核,登录密码为手机号！',
							type: 'success',
							duration: '2300'
						})
						setTimeout(() => {
							// this.tologin(this.regedit);
							this.goHome(this.regedit)
						}, 2300);
					} else if (res.result.status == 2) {
						clearInterval(this.timer);
						this.$refs.uToast.show({
							title: '申请已被驳回！请返回登录',
							type: 'warning',
							duration: '2300'
						});
					}
				})
			},
			toindex() {
				wx.vibrateShort();
				uni.navigateTo({
					url: `/pages/index/index`
				})
			},
			tologin(regedit) {
				uni.navigateTo({
					url: '/pages/index/index?usercode=' + regedit.mobile + '&pwd=' + regedit.newpwd
				})
			},
			goHome(regedit) {
				let params = {
					mobile: regedit.mobile,
					pwd: ''
				};
				this.$api.post({
					url: '/sysuser/mobilelogin'
				}, params).then(res => {
					if (res.code == "0") {
						//登录成功后，存储账号密码
						uni.setStorageSync('accont', params)
						//console.log(uni.getStorageSync('accont'));
						switch (res.result) {
							case 'password_error':
								this.$refs.uToast.show({
									title: '密码错误',
									type: 'warning',
								});
								break;
							case 'none':
								this.$refs.uToast.show({
									title: '无此用户，请先注册',
									type: 'warning',
								});
								break;
							case 'overtime':
								this.$refs.uToast.show({
									title: '服务到期，请联系学校管理员',
									type: 'warning',
								});
								break;
							default:
								uni.setStorageSync("user", res.user);
								uni.setStorageSync("roles", res.roles);
								//console.log(res.roles)
								uni.setStorageSync('role', res.roles[0]);
								//console.log(">>>>>unionid    " + this.unionid)
								//console.log(res.user)
								if (res.user.unionid == '') {
									this.updateUser(res.user.id, res.roles); //更新用户unionid信息
								} else {
									this.tohome(res.roles);
								}
								break;
						}
					}
				})
			},
			tohome(roles) {
				if (roles.length == 1) {
					let ur = uni.getStorageSync("user");
					if (roles[0] == 'tr') {
						this.$api.post({
							url: '/yschool/getById'
						}, {
							id: ur.schoolId
						}).then(res => {
							uni.setStorageSync("currSchool", res.school)
							uni.reLaunch({
								url: '/pages/home/home'
							})
						})
					} else if (roles[0] == 'stu') {
						uni.reLaunch({
							url: '/pageHome/stuhome/home'
						})
					} else if (roles[0] == 'team') {
						let _this = this;
						//agree
						wx.requestSubscribeMessage({
							tmplIds: ['D1_iDgju0SYeeckTOOWkgndTWQpr6hLNdHZDZxlPmj4'],
							success(rs) {
								if (JSON.stringify(rs).indexOf('accept') > 0) {
									//console.log("同意");
								} else {
									//console.log("拒绝")
								}
								_this.$api.post({
									url: '/yschool/getById'
								}, {
									id: ur.schoolId
								}).then(res => {
									uni.setStorageSync("userSchool", res.school)
									uni.reLaunch({
										url: '/team/kc/changeSchool'
									})
								})
							},
							fail(rs) {
								//console.log(rs)
							},
						})
					} else if (roles[0] == 'admin') {
						this.$api.post({
							url: '/yschool/getById'
						}, {
							id: ur.schoolId
						}).then(res => {
							uni.setStorageSync("currSchool", res.school)
							uni.reLaunch({
								url: '/team/schoolHome'
							})
						})
					}
				} else if (roles.length > 1) {
					uni.reLaunch({
						url: '/pages/index/selectRole'
					})
				}
			},
			updateUser(userid, roles) {
				this.$api.post({
					url: '/sysuser/modifyUnionid'
				}, {
					id: userid,
					appletsOpenid: this.openid,
					unionId: this.unionid
				}).then(s => {
					// //console.log(">>update user unionid success");
					this.tohome(roles);
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.shr {
		color: #233192;
		font-size: 14px;
		text-align: center;
	}

	.main-cont {
		width: 100%;
		height: 100%;
	}

	.content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: url(https://yh-sys.oss-cn-beijing.aliyuncs.com/6B9AF39796704A5CB130FC9C0CFB7A28icon_bg.png?Expires=4799124463&OSSAccessKeyId=LTAI4GL8SwAiLh3sSPXDS7z9&Signature=3z2ea85buEEsjoSlp5uw2FIOMoo%3D) no-repeat;
		background-size: 100%;
		color: #fff;
		background-color: #888bf3;
	}

	.shz {
		margin: 0 auto;
		color: #181623;
		font-size: 36rpx;
		text-align: center;

		>.bo {}
	}

	.shenhe {
		width: 630rpx;
		height: 500rpx;
		color: #181623;
		font-size: 36rpx;
		margin: 0 auto;

		&>image {
			margin: 30rpx auto;
			display: block;
		}
	}

	.fankun {
		position: absolute;
		bottom: 250rpx;
		margin: 0 auto;
		width: 630rpx;
		height: 320rpx;
		background: #FFFFFF;
		opacity: .08;
		border-radius: 8rpx;
	}

	.text {
		padding: 10px;
		min-height: 320rpx;
		height: auto !important;
		overflow: hidden !important;
		overflow: hidden;
		width: 630rpx;
		height: 320rpx;
		position: absolute;
		bottom: 250rpx;
		font-size: 40rpx;
		color: #44467a;
		text-indent: 2em;
		z-index: 10;
		word-wrap: break-word;
	}

	.statusimg {
		width: 80px;
		height: 80px;
	}

	.bo {
		text-align: center;
		width: 630rpx;
		color: #233192;
		font-weight: bold;
	}

	.register {
		position: absolute;
		bottom: 20px;
		text-align: center;
		width: 500rpx;
		height: 30rpx;
	}
</style>
